package com.lfz.quality_effect.utils;

import lombok.Data;

@Data
public class ResultData<T> {

    // 结果状态码
    private int code;

    // 响应信息
    private String msg;

    // 响应数据
    private T data;

    // 接口请求时间
    private long timestamp ;

    private int total;

    public ResultData(){
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> ResultData<T> success(T data){
        ResultData resultData = new ResultData();
        resultData.setCode(ReturnCode.RC200.getCode());
        resultData.setMsg(ReturnCode.RC200.getMsg());
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> fail(int code, String msg){
        ResultData resultData = new ResultData();
        resultData.setCode(code);
        resultData.setMsg(msg);
        return resultData;
    }
    public static <T> ResultData<T> fail(int code, T data){
        ResultData resultData = new ResultData();
        resultData.setCode(code);
        resultData.setData(data);
        resultData.setMsg(ReturnCode.RC500.getMsg());
        return resultData;
    }
    public static <T> ResultData<T> success(String message){
        ResultData resultData = new ResultData();
        resultData.setCode(ReturnCode.RC200.getCode());
        resultData.setMsg(message);
        resultData.setData(null);
        return resultData;
    }

    public ResultData total(int total){
        this.total = total;
        return this;
    }
    public ResultData data(T data){
        this.data=data;
        return this;
    }

    /**
     * 成功静态方法
     * @return
     */
    public static ResultData ok() {
        ResultData r = new ResultData();
        r.setCode(ReturnCode.RC200.getCode());
        r.setMsg("成功");
        return r;
    }
}
